package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.PageInfo;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetmealDishDto;
import com.itheima.dto.SetmealDishDto2;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.SetmealService;
import com.itheima.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageInfo<SetmealVo> page(String name, Integer page, Integer pageSize) {
        PageInfo<SetmealVo> pageInfo = new PageInfo<>();
        Integer start = (page - 1) * pageSize;
        List<Setmeal> setmeals = setmealMapper.page(name, start, pageSize);
        List<SetmealVo> setmealVos = new ArrayList<>();
        for (Setmeal setmeal : setmeals) {
            SetmealVo setmealVo = new SetmealVo();
            BeanUtils.copyProperties(setmeal, setmealVo);
            String categoryName = setmealMapper.selectCategoryNameByCategoryId(setmeal.getCategoryId());
            setmealVo.setCategoryName(categoryName);
            setmealVos.add(setmealVo);
        }
        pageInfo.setRecords(setmealVos);
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        pageInfo.setTotal(setmealMapper.countSetmeal(name));
        return pageInfo;
    }

    @Override
    public Boolean addSetmeal(SetmealDishDto setmealDishDto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDishDto, setmeal);
        setmeal.setId(IdUtil.getSnowflakeNextIdStr());
        Integer res = setmealMapper.insertSetmeal(setmeal);

        List<DishDto> setmealDishes = setmealDishDto.getSetmealDishes();
        if (!CollectionUtils.isEmpty(setmealDishes)) {
            for (DishDto dishDto : setmealDishes) {
                if (dishDto == null) {
                    continue;
                }

                SetmealDish setmealDish = new SetmealDish();
                BeanUtils.copyProperties(dishDto, setmealDish);
                setmealDish.setId(IdUtil.getSnowflakeNextIdStr());
                setmealDish.setSetmealId(setmeal.getId());

                List<SetmealDish> newSetmealDishes = new ArrayList<>();
                newSetmealDishes.add(setmealDish);

                Integer r = setmealMapper.insertDish(newSetmealDishes);
                if (r != newSetmealDishes.size()) {
                    return false;
                }
            }
        }
        return res == 1;
    }

    @Override
    public SetmealVo2 getById(String id) {
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        SetmealVo2 setmealVo2 = new SetmealVo2();
        BeanUtils.copyProperties(setmeal, setmealVo2);

        List<SetmealDish> setmealDishes = setmealMapper.selectDishBySetmealId(id);
        setmealVo2.setSetmealDishes(setmealDishes);

        setmealVo2.setCategoryName(setmealMapper.selectCategoryNameByCategoryId(setmeal.getId()));

        return setmealVo2;
    }

    @Override
    public Boolean modifySetmeal(SetmealDishDto2 setmealDishDto2) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDishDto2, setmeal);

        setmeal.setUpdateTime(LocalDateTime.now());

        Integer result = setmealMapper.update(setmeal);
        if (result == 0) {
            return false;
        }

        Integer total = setmealMapper.countDishBySetmealId(setmealDishDto2.getId());
        if (total > 0) {
            Integer removeResult = setmealMapper.removeDishBySetmealId(setmealDishDto2.getId());
            if (!removeResult.equals(total)) {
                throw new RuntimeException("菜品删除失败");
            }
        }
        List<DishDto> setmealDishes = setmealDishDto2.getSetmealDishes();
        List<SetmealDish> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(setmealDishes)) {
            for (DishDto dishDto : setmealDishes) {
                if (dishDto == null) {
                    continue;
                }
                SetmealDish setmealDish = new SetmealDish();
                BeanUtils.copyProperties(dishDto, setmealDish);
                setmealDish.setId(IdUtil.getSnowflakeNextIdStr());
                setmealDish.setSetmealId(setmeal.getId());

                list.add(setmealDish);
            }
        }
        Integer res = setmealMapper.insertSetmealDish(list);
        if (res != list.size()) {
            throw new RuntimeException("菜品添加失败");
        }
        return result == 1;
    }

    @Override
    public Boolean delete(String ids) {
        String[] setmealIds = ids.split(",");
        Integer result = setmealMapper.deleteSetmeal(setmealIds);
        if (result > 0) {
            Integer res = setmealMapper.deleteSetmealDish(setmealIds);
            // if (res != setmealIds.length) {
            //     throw new RuntimeException("套餐菜品删除失败");
            // }
        }
        return result == ids.length();
    }

    @Override
    public Boolean status(Integer status, String ids) {
        String[] setmealIds = ids.split(",");
        Integer res = setmealMapper.updateStatusByIds(status, setmealIds);
        return res == setmealIds.length;
    }

    @Override
    public List<PhoneSetmealVo> getPhoneSetmealVo(String categoryId, Integer status) {
        ArrayList<PhoneSetmealVo> phoneSetmealVoList = new ArrayList<>();

        List<Setmeal> setmealList = setmealMapper.selectSetmeal(categoryId,status);
        for (Setmeal setmeal : setmealList) {
            PhoneSetmealVo phoneSetmealVo = new PhoneSetmealVo();
            BeanUtils.copyProperties(setmeal, phoneSetmealVo);
            phoneSetmealVoList.add(phoneSetmealVo);
        }
        return phoneSetmealVoList;
    }

    @Override
    public PhoneSetmealDishVo getPhoneSetmealDish(String id) {
        PhoneSetmealDishVo phoneSetmealDishVo = new PhoneSetmealDishVo();
        Setmeal setmeal = setmealMapper.selectSetmealById(id);
        BeanUtils.copyProperties(setmeal, phoneSetmealDishVo);
        List<SetmealDish> setmealDishes = setmealMapper.selectDishBySetmealId(id);
        phoneSetmealDishVo.setSetmealDishes(setmealDishes);
        return phoneSetmealDishVo;
    }

}
